﻿/**
* @requires jquery.validate.js
* @author ZhangHuihua@msn.com
*/
(function ($) {
    if ($.validator) {
        $.validator.addMethod("alphanumeric", function (value, element) {
            return this.optional(element) || /^\w+$/i.test(value);
        }, "Letters, numbers or underscores only please");

        $.validator.addMethod("lettersonly", function (value, element) {
            return this.optional(element) || /^[a-z]+$/i.test(value);
        }, "Letters only please");

        $.validator.addMethod("phone", function (value, element) {
            return this.optional(element) || /^[0-9 \(\)]{7,30}$/.test(value);
        }, "Please specify a valid phone number");

        $.validator.addMethod("postcode", function (value, element) {
            return this.optional(element) || /^[0-9 A-Za-z]{5,20}$/.test(value);
        }, "Please specify a valid postcode");
        $.validator.addMethod("float", function (value, element) {
            return this.optional(element) || /^\d+(\.\d+)?$/.test(value);
        }, "请输入正确的浮点数[0.0]");        
        $.validator.addMethod("date", function (value, element) {
            value = value.replace(/\s+/g, "");
            if (String.prototype.parseDate) {
                var $input = $(element);
                var pattern = $input.attr('format') || 'yyyy-MM-dd';

                return !$input.val() || $input.val().parseDate(pattern);
            } else {
                return this.optional(element) || value.match(/^\d{4}[\/-]\d{1,2}[\/-]\d{1,2}$/);
            }
        }, "Please enter a valid date.");
        $.validator.addMethod("Tel", function (value, element) {
            return this.optional(element) || /^1[3|4|5|8][0-9]\d{4,8}$/.test(value);
        }, "请输入正确手机号码");

        $.validator.addClassRules({
            date: { date: true },
            alphanumeric: { alphanumeric: true },
            lettersonly: { lettersonly: true },
            phone: { phone: true },
            postcode: { postcode: true },
            Tel: { Tel: true }
        });
        $.validator.setDefaults({ errorElement: "span" });
        $.validator.autoCreateRanges = true;

    }

})(jQuery);